﻿Imports System.Collections.Generic

Partial Class WebForm_SearchRoleUserMappingByUser
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Visible = True
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Me.Show(Me.UserID)
        'Me.SearchRoleControl1.Show(Me.UserID)
    End Sub

    Protected Sub SearchRoleControl1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchRoleControl1.Saving
        Me.SaveData()
        Me.Show(Me.UserID)
    End Sub

    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        If Me.DeleteData(e.RowIndex) = True Then
            Me.Show(Me.UserID)
        End If
    End Sub
End Class

Partial Class WebForm_SearchRoleUserMappingByUser
    Public Sub Show(ByVal userID As String)
        Me.UserID = userID
        Me.BindData()
        Me.BindGV()

        Me.mpSearch.Show()
        Me.Visible = True
    End Sub

    Public Sub ShowReadOnly(ByVal userID As String)
        Me.Show(userID)

        Me.btnAdd.Visible = False
        Me.gv.Columns(0).Visible = False
    End Sub

    Private Sub BindData()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select u.* ")
        sql.AppendFormat("from [User] u ")
        sql.AppendFormat("where u.UserID = '{0}' ", Me.UserID)
        Dim dt As Data.DataTable = dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt").Tables(0)

        Me.OrganizationID = dt.Rows(0)("OrganizationID")
        Me.lblForm.Text = dt.Rows(0)("UserName") + " " + Me.GetLocalResourceObject("Page.Text")

        If clsAuth.GetAuth(Me.OrganizationID, "Add") = False Then
            Me.btnAdd.Enabled = False
        Else
            Me.btnAdd.Enabled = True
        End If

        If clsAuth.GetAuth(Me.OrganizationID, "Delete") = False Then
            Me.gv.Columns(0).Visible = False
        Else
            Me.gv.Columns(0).Visible = True
        End If

    End Sub

    Private Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select ru.* ")
        sql.AppendFormat(", r.RoleName ")
        sql.AppendFormat("from RoleUserMapping ru ")
        sql.AppendFormat("inner join Role r on ru.RoleID = r.RoleID ")
        sql.AppendFormat("where ru.UserID = '{0}' ", Me.UserID)
        Me.gv.DataSource = dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt")
        Me.gv.DataBind()
    End Sub

    Private Function SaveData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            For Each roleID As String In Me.SearchRoleControl1.SelectedRoles
                sql.Length = 0
                sql.AppendFormat("insert RoleUserMapping (RoleID, UserID) select ")
                sql.AppendFormat(" '{0}' ", roleID)
                sql.AppendFormat(",'{0}' ", Me.UserID)
                dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            Next
            scope.Complete()
        End Using

        Return True
    End Function

    Private Function DeleteData(ByVal rowIndex As Integer) As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Dim row As GridViewRow = Me.gv.Rows(rowIndex)
        Dim key As DataKey = Me.gv.DataKeys(rowIndex)

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("delete RoleUserMapping where ")
            sql.AppendFormat(" RoleID='{0}' ", key("RoleID"))
            sql.AppendFormat("and UserID='{0}' ", key("UserID"))
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            scope.Complete()
        End Using

        Return True
    End Function

    Public Property UserID() As String
        Get
            Return ViewState("UserID")
        End Get
        Set(ByVal value As String)
            ViewState("UserID") = value
        End Set
    End Property

    Public Property OrganizationID() As String
        Get
            Return ViewState("OrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("OrganizationID") = value
        End Set
    End Property

End Class